package com.cskaoyan.controller;

import com.cskaoyan.bean.PO.MarketAddress;
import com.cskaoyan.bean.PO.MarketUser;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.service.WxAddressService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author:zhangjiaxin
 **/
@RestController
@RequestMapping("wx/address")
public class WxAddressController {


    @Autowired
    WxAddressService wxAddressService;

    @RequestMapping("list")
    public BaseRespVo list() {
        //获取当前用户的信息
        Subject subject = SecurityUtils.getSubject();
        //拿到当前用户
        PrincipalCollection principals = subject.getPrincipals();
        if (principals == null) {
            return BaseRespVo.error(501, "需要重新登陆");
        }
        MarketUser currentUser = (MarketUser) principals.getPrimaryPrincipal();
        CommonData<MarketAddress> data = wxAddressService.queryAddressList(currentUser);
        return BaseRespVo.ok(data);
    }

    @RequestMapping("detail")
    public BaseRespVo detail(Integer id) {
        MarketAddress address = wxAddressService.queryByAddressDetail(id);
        return BaseRespVo.ok(address);
    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody MarketAddress address) {
        Integer affactRows = wxAddressService.deleteById(address);
        if (affactRows == null) {
            return BaseRespVo.invalidData();
        }
        return BaseRespVo.ok();
    }

    @RequestMapping("save")
    public BaseRespVo save(@RequestBody MarketAddress marketAddress){
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser  = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = marketUser.getId();
        Integer id = wxAddressService.save(marketAddress,userId);
        return BaseRespVo.ok(id);
    }


}
